package vn.orderonline.com.core.business.admin;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;

import vn.orderonline.com.core.business.dto.AdminDto;
import vn.orderonline.com.core.business.dto.RestResponseDto;
import vn.orderonline.com.core.business.dto.ReturnCode;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.dao.AdminDao;
import vn.orderonline.core.dao.CityDao;
import vn.orderonline.core.dao.DistrictDao;
import vn.orderonline.core.dao.RoleDao;
import vn.orderonline.core.dao.entity.Admin;
import vn.orderonline.core.dao.entity.Role;

@Service
@Transactional
public class AdminImpl implements IAdmin {

	@Autowired
	private AdminDao adminDao;

	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private CityDao cityDao;
	
	@Autowired
	private DistrictDao districtDao;

	@Override
	public List<AdminDto> getAllAdmin() {

		List<Object[]> list = adminDao.getAllAdmin();
		List<AdminDto> listDto = new ArrayList<>();

		AdminDto adminDto = null;

		if (!list.isEmpty()) {
			for (Object[] obj : list) {
				Admin admin = (Admin) obj[0];
				Role role = (Role) obj[1];
				adminDto = new AdminDto();

				adminDto.setAdminId(admin.getAdminId());
				adminDto.setAdminName(admin.getAdminName());
				adminDto.setAdminEmail(admin.getAdminEmail());
				adminDto.setAdminLoginId(admin.getAdminLoginId());
				adminDto.setAdminPassword(admin.getAdminPassword());
				if (admin.getAdminGender() != null) {
					adminDto.setAdminGender(admin.getAdminGender() == true ? "Male" : "Female");
				}
				adminDto.setPhoneNumber(admin.getPhoneNumber());
				adminDto.setDistrictId(admin.getDistrictId());
				if (admin.getDistrictId() != null) {
					adminDto.setDistrictName(districtDao.getDistrictNameByDistrictId(admin.getDistrictId()));
				}
				adminDto.setCityId(admin.getCityId());
				if (admin.getCityId() != null) {
					adminDto.setCityName(cityDao.getCityNameByCityId(admin.getCityId()));
				}
				adminDto.setCreateId(admin.getCreateId());
				adminDto.setCreateTime(DateUtils.timestampToString(admin.getCreateTime(), "dd/MM/yyyy HH:mm:ss"));
				adminDto.setUpdateId(admin.getUpdateId());
				if (admin.getUpdateTime() != null) {
					adminDto.setUpdateTime(DateUtils.timestampToString(admin.getUpdateTime(), "dd/MM/yyyy HH:mm:ss"));
				}
				adminDto.setRoleId(admin.getRoleId());
				adminDto.setRoleId(role.getRoleId());
				adminDto.setRoleName(role.getRoleName());

				listDto.add(adminDto);
			}
		}
		return listDto;
	}

	@Override
	public AdminDto getAdminByAdminLoginId(String adminLoginId) {
		Object[] obj = adminDao.getAdminByAdminLoginId(adminLoginId);
		AdminDto adminDto = null;
		if (!StringUtils.isNullOrEmpty(adminLoginId)) {
			if (obj != null) {
				Admin admin = (Admin) obj[0];
				Role role = (Role) obj[1];
				adminDto = new AdminDto();
				
				adminDto.setAdminId(admin.getAdminId());
				adminDto.setAdminName(admin.getAdminName());
				adminDto.setAdminEmail(admin.getAdminEmail());
				adminDto.setAdminLoginId(admin.getAdminLoginId());
				adminDto.setAdminPassword(admin.getAdminPassword());
				if (admin.getAdminBirthday() != null) {
					adminDto.setAdminBirthday(DateUtils.dateToString(admin.getAdminBirthday(), "dd/MM/yyyy"));
				}
				if (admin.getAdminGender() != null) {
					adminDto.setAdminGender(admin.getAdminGender() == true ? "Male" : "Female");
				}
				adminDto.setPhoneNumber(admin.getPhoneNumber());
				adminDto.setDistrictId(admin.getDistrictId());
				if (admin.getDistrictId() != null) {
					adminDto.setDistrictName(districtDao.getDistrictNameByDistrictId(admin.getDistrictId()));
				}
				adminDto.setCityId(admin.getCityId());
				if (admin.getCityId() != null) {
					adminDto.setCityName(cityDao.getCityNameByCityId(admin.getCityId()));
				}
				adminDto.setCreateId(admin.getCreateId());
				adminDto.setCreateTime(DateUtils.timestampToString(admin.getCreateTime(), "dd/MM/yyyy HH:mm:ss"));
				adminDto.setUpdateId(admin.getUpdateId());
				if (admin.getUpdateTime() != null) {
					adminDto.setUpdateTime(DateUtils.timestampToString(admin.getUpdateTime(), "dd/MM/yyyy HH:mm:ss"));
				}
				adminDto.setRoleId(admin.getRoleId());
				
				adminDto.setRoleId(role.getRoleId());
				adminDto.setRoleName(role.getRoleName());
			}
		}
		return adminDto;
	}

	@Override
	public RestResponseDto addAdmin(AdminDto admin) {
		Admin ad = new Admin();
		ad.setAdminName(admin.getAdminName());
		ad.setAdminEmail(admin.getAdminEmail());
		ad.setAdminLoginId(admin.getAdminLoginId());
		ad.setAdminPassword(admin.getAdminPassword());
		if (!StringUtils.isNullOrEmpty(admin.getAdminBirthday())) {
			ad.setAdminBirthday(new Date((DateUtils.stringToDate(
							admin.getAdminBirthday(), "yyyy-MM-dd").getTime())));
		}
		ad.setPhoneNumber(admin.getPhoneNumber());
		if (!StringUtils.isNullOrEmpty(admin.getAdminGender())) {
			ad.setAdminGender(admin.getAdminGender() == "Male" ? true : false);
		}
		ad.setCityId(admin.getCityId());
		ad.setDistrictId(admin.getDistrictId());
		ad.setCreateId("zuchihashishui");
		ad.setCreateTime(new Timestamp(DateUtils.now()));
		ad.setRoleId(admin.getRoleId());
		adminDao.save(ad);
		return new RestResponseDto(ReturnCode.SUCCESS, "SAVE SUCCESS!");
	}
	
	@Override
	public Boolean checkExistedByEmail(String adminEmail) {
		return adminDao.checkExistedEmail(adminEmail);
	}

	@Override
	public Boolean checkExistedByAdminLoginId(String adminLoginId) {
		return adminDao.checkExistedAdminLoginId(adminLoginId);
	}

	@Override
	public Boolean checkExistedAccount(String adminLoginId,
			String password) {
		if (!StringUtils.isNullOrEmpty(adminLoginId) || !StringUtils.isNullOrEmpty(password)) {
			if (adminDao.checkExistedAccount(adminLoginId.trim(), password.trim())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public RestResponseDto delete(String adminLoginId) {
		Object[] obj = adminDao.getAdminByAdminLoginId(adminLoginId);
		if (!StringUtils.isNullOrEmpty(adminLoginId)) {
			if (obj != null) {
				Admin admin = (Admin) obj[0];
				adminDao.delete(admin);
			}
		}
		return new RestResponseDto(ReturnCode.SUCCESS, "DELETE SUCCESS!");
	}

//	@Override
//	public List<AdminDto> search(String adminName, String adminEmail,
//			String adminLoginId, String adminPassword) {
//		List<AdminDto> listDto = new ArrayList<AdminDto>();
//
//		List<Admin> listEntity = adminDao.search(adminName, adminEmail,
//				adminLoginId, adminPassword);
//
//		if (!listEntity.isEmpty()) {
//			AdminDto adminDto = new AdminDto();
//			for (Admin admin : listEntity) {
//				adminDto.setAdminId(admin.getAdminId());
//				adminDto.setAdminName(admin.getAdminName());
//				adminDto.setAdminEmail(admin.getAdminEmail());
//				adminDto.setAdminLoginId(admin.getAdminLoginId());
//				adminDto.setAdminPassword(admin.getAdminPassword());
//				adminDto.setCreateId(admin.getCreateId());
//				adminDto.setCreateTime(admin.getCreateTime());
//				adminDto.setUpdateId(admin.getUpdateId());
//				adminDto.setUpdateTime(admin.getUpdateTime());
//				adminDto.setRoleId(admin.getRoleId());
//
//				listDto.add(adminDto);
//			}
//		}
//		return listDto;
//	}
//

//
//	@Override
//	public void update(AdminDto admin) {
//
//		if (admin != null) {
//			Admin adminEntity = adminDao.find(admin.getAdminId());
//
//			adminEntity.setAdminId(admin.getAdminId());
//			adminEntity.setAdminName(admin.getAdminName());
//			adminEntity.setAdminEmail(admin.getAdminEmail());
//			adminEntity.setAdminLoginId(admin.getAdminLoginId());
//			adminEntity.setAdminPassword(admin.getAdminPassword());
//			adminEntity.setUpdateId(admin.getUpdateId());
//			adminEntity.setUpdateTime(new Timestamp(DateUtils.now()));
//			adminEntity.setRoleId(admin.getRoleId());
//
//			adminDao.update(adminEntity);
//		}
//	}
//


}
